package com.ssyt.hiring.web.action;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ssyt.cdp.common.util.FastjsonUtil;
import com.ssyt.hiring.entity.ApplicationEntity;
import com.ssyt.hiring.entity.ResumeEntity;
import com.ssyt.hiring.entity.TalentEntity;
import com.ssyt.hiring.web.service.TalentService;

@Controller
@Scope("prototype")
public class TalentAction extends CommonAction {

	@Autowired
	private TalentService talentService;
	private List<ResumeEntity> resumeList;
	private List<ApplicationEntity> applicationList;
	private TalentEntity talent;
	private int talentId;
	private int resumeId;

	/**
	 * 求职者登录
	 * 
	 * @return
	 */
	public String talentLogin() {
		talent = talentService.talentLogin(talent);
		if (talent != null) {
			ServletActionContext.getRequest().getSession()
					.setAttribute("talent", talent);
			resumeList = getTalentResume();
			applicationList = getApplicationListInfo();
			return "talentMain";
		} else {
			return "talentLogin";
		}
	}

	/**
	 * 求职者注销
	 * 
	 * @return
	 */
	public String logout() {

		talentService.logout();
		return "Logout";

	}

	/**
	 * 求职者注册
	 * 
	 * @return
	 */
	public String talentRegister() {

		talentService.talentRegister(talent);
		return "talentLogin";
	}

	/**
	 * 查询求职者信息
	 * 
	 * @return
	 */

	public String getTalentInfo() {

		int talentId = getTalentId();
		talentService.getTalentInfo(talentId);
		return "talent-Info";
	}

	public int getTalentId() {
		TalentEntity talent = (TalentEntity) ServletActionContext.getRequest()
				.getSession().getAttribute("talent");
		return talent.getId();
	}

	/**
	 * 求职者资料修改
	 * 
	 * @return
	 */
	public String talentUpdate() {
		talentService.talentUpdate(talent);
		return "talent-Info";

	}

	/**
	 * 求职者简历查询
	 * 
	 * @return
	 */
	public List<ResumeEntity> getTalentResume() {
		int myTalentId = getTalentId();
		return resumeList = talentService.getTalentResume(myTalentId);
	}

	public List<ApplicationEntity> getApplicationListInfo() {
		return applicationList = talentService.getApplicationListInfo();

	}

	/**
	 * 跳转到求职者查询页面
	 * 
	 * @return
	 */
	public String getTalentResumeUI() {
		int myTalentId = getTalentId();
		resumeList = talentService.getTalentResume(myTalentId);
		applicationList = getApplicationListInfo();
		return "talentMain";
	}

	/**
	 * Ajax查询所有的求职者信息
	 */
	public void getTalentInfoByAjax() {
		talent = talentService.getTalentInfoByAjax(resumeId, talentId);
		try {
			ServletActionContext.getResponse().getWriter()
					.print(FastjsonUtil.toJson(talent));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void checkTalentName() {
		boolean resuslt = talentService.checkTalentName(talent.getCode());
		print(resuslt + "");
	}

	public TalentEntity getTalent() {
		return talent;
	}

	public void setTalent(TalentEntity talent) {
		this.talent = talent;
	}

	public int getResumeId() {
		return resumeId;
	}

	public void setResumeId(int resumeId) {
		this.resumeId = resumeId;
	}

	public TalentService getTalentService() {
		return talentService;
	}

	public void setTalentService(TalentService talentService) {
		this.talentService = talentService;
	}

	public void setTalentId(int talentId) {
		this.talentId = talentId;
	}

	public List<ResumeEntity> getResumeList() {
		return resumeList;
	}

	public void setResumeList(List<ResumeEntity> resumeList) {
		this.resumeList = resumeList;
	}

	public List<ApplicationEntity> getApplicationList() {
		return applicationList;
	}

	public void setApplicationList(List<ApplicationEntity> applicationList) {
		this.applicationList = applicationList;
	}

}
